package com.vint.leetcode;

import com.vint.common.TreeNode;

/*
 * 1. done
 * 2. easy
 * 3. tree, 递归
 * 判断树是否自我对称
 * ~左右子树反过来，再判断是否相等
 */
public class Leetcode101 {
    public boolean isSymmetric(TreeNode root) {
        if(null==root)return true;
        return isSameTree(invertTree(root.left),root.right);
    }
    //判断是否相等
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(null==p||null==q){
            return p==q;
        }
        if(p.val==q.val){
            return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
        }
        return false;

    }
    //对调左右
    public TreeNode invertTree(TreeNode root) {
        if(null==root)return root;
        TreeNode left = root.left;
        root.left = invertTree(root.right);
        root.right = invertTree(left);
        return root;
    }

    public static void main(String[] args) {
        Leetcode101 leetcode101= new Leetcode101();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(3);
        root.left.left = new TreeNode(5);
        root.right = new TreeNode(2);
        root.right.right= new TreeNode(5);
        System.out.println(leetcode101.isSameTree(root,root));
    }
  }
